﻿using AyuBookmark.Common.Models;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace AyuBookmark.WinForm.Dialogs
{
    /// <summary>
    /// 分类编辑对话框
    /// </summary>
    public partial class CategoryEditor : Form
    {
        private TextBox txtName;
        private Button btnColor;
        private Button btnSave;
        private Button btnCancel;
        private Panel colorPanel;
        private Label lblName;
        private Label lblColor;
        private ComboBox cmbParent;
        private Label lblParent;

        public Category CurrentCategory { get; private set; }

        public CategoryEditor(Category category = null, int? parentId = null)
        {
            InitializeComponent();
            CurrentCategory = category ?? new Category
            {
                Color = "#4285F4",
                ParentId = parentId
            };
            InitializeUI();
        }

        private void InitializeComponent()
        {
            // 窗体基础设置
            this.SuspendLayout();
            this.Text = CurrentCategory.Id == 0 ? "添加分类" : "编辑分类";
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ClientSize = new Size(350, 200);
            this.BackColor = SystemColors.Window;
            this.Padding = new Padding(10);

            // 名称标签
            this.lblName = new Label();
            this.lblName.Text = "名称:";
            this.lblName.AutoSize = true;
            this.lblName.Location = new Point(20, 20);
            this.lblName.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // 名称文本框
            this.txtName = new TextBox();
            this.txtName.Location = new Point(100, 17);
            this.txtName.Size = new Size(220, 25);
            this.txtName.Text = CurrentCategory.Name;
            this.txtName.Font = new Font("Microsoft YaHei", 9f);

            // 颜色标签
            this.lblColor = new Label();
            this.lblColor.Text = "颜色:";
            this.lblColor.AutoSize = true;
            this.lblColor.Location = new Point(20, 60);
            this.lblColor.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // 颜色面板
            this.colorPanel = new Panel();
            this.colorPanel.BackColor = ColorTranslator.FromHtml(CurrentCategory.Color);
            this.colorPanel.BorderStyle = BorderStyle.FixedSingle;
            this.colorPanel.Location = new Point(100, 57);
            this.colorPanel.Size = new Size(25, 25);

            // 颜色按钮
            this.btnColor = new Button();
            this.btnColor.Text = "选择...";
            this.btnColor.Size = new Size(80, 25);
            this.btnColor.Location = new Point(140, 57);
            this.btnColor.Font = new Font("Microsoft YaHei", 9f);
            this.btnColor.Click += (s, e) => SelectColor();

            // 父分类标签
            this.lblParent = new Label();
            this.lblParent.Text = "父分类:";
            this.lblParent.AutoSize = true;
            this.lblParent.Location = new Point(20, 100);
            this.lblParent.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);

            // 父分类下拉框
            this.cmbParent = new ComboBox();
            this.cmbParent.Location = new Point(100, 97);
            this.cmbParent.Size = new Size(220, 25);
            this.cmbParent.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbParent.Font = new Font("Microsoft YaHei", 9f);

            // 按钮面板
            var panel = new Panel();
            panel.Dock = DockStyle.Bottom;
            panel.Height = 50;
            panel.BackColor = SystemColors.Control;

            // 保存按钮
            this.btnSave = new Button();
            this.btnSave.Text = "保存";
            this.btnSave.DialogResult = DialogResult.OK;
            this.btnSave.Size = new Size(80, 30);
            this.btnSave.Location = new Point(150, 10);
            this.btnSave.Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);
            this.btnSave.Click += (s, e) => SaveCategory();

            // 取消按钮
            this.btnCancel = new Button();
            this.btnCancel.Text = "取消";
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Size = new Size(80, 30);
            this.btnCancel.Location = new Point(240, 10);
            this.btnCancel.Font = new Font("Microsoft YaHei", 9f);

            // 添加控件到窗体
            this.Controls.Add(this.lblName);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.lblColor);
            this.Controls.Add(this.colorPanel);
            this.Controls.Add(this.btnColor);
            this.Controls.Add(this.lblParent);
            this.Controls.Add(this.cmbParent);
            this.Controls.Add(panel);
            panel.Controls.Add(this.btnSave);
            panel.Controls.Add(this.btnCancel);

            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void InitializeUI()
        {
            // 加载父分类选项
            this.cmbParent.Items.Add("(无)");
            // 这里应该加载可选的父分类列表
            // cmbParent.Items.AddRange(...);

            if (CurrentCategory.ParentId.HasValue)
            {
                // 设置当前父分类选择
                // cmbParent.SelectedItem = ...
            }
            else
            {
                this.cmbParent.SelectedIndex = 0;
            }
        }

        //private void SelectColor()
        //{
        //    this.colorDialog.Color = this.colorPanel.BackColor;
        //    if (this.colorDialog.ShowDialog() == DialogResult.OK)
        //    {
        //        this.colorPanel.BackColor = this.colorDialog.Color;
        //    }
        //}

        private ColorDialog colorDialog = new ColorDialog();

        private void SelectColor()
        {
            colorDialog.Color = colorPanel.BackColor;
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                colorPanel.BackColor = colorDialog.Color;
            }
        }

        private void SaveCategory()
        {
            if (string.IsNullOrWhiteSpace(this.txtName.Text))
            {
                MessageBox.Show("请输入分类名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtName.Focus();
                return;
            }

            CurrentCategory.Name = this.txtName.Text.Trim();
            CurrentCategory.Color = ColorTranslator.ToHtml(this.colorPanel.BackColor);

            if (this.cmbParent.SelectedIndex > 0)
            {
                // CurrentCategory.ParentId = 选择的父分类ID
            }
            else
            {
                CurrentCategory.ParentId = null;
            }
        }

        public static DialogResult ShowEditor(IWin32Window owner, Category category = null)
        {
            using (var dialog = new CategoryEditor(category))
            {
                return dialog.ShowDialog(owner);
            }
        }
    }
}